/**
 * <pre>
 * Title: 		DomainObjectEditor.java
 * Author:		linriqing
 * Create:	 	2010-8-6 上午11:39:35
 * Copyright: 	Copyright (c) 2010
 * Company:		Shenzhen Helper
 * <pre>
 */
package com.hengpeng.itfinoss.domain;

import java.util.Collection;

import org.springframework.beans.propertyeditors.CustomCollectionEditor;

import com.hengpeng.common.persist.entity.AbstractEntity;
import com.hengpeng.itfinoss.service.ServiceFactory;

/**
 * <pre>
 * 领域模型对象集合转换器
 * </pre>
 * @author linriqing
 * @version 1.0, 2010-8-6
 */
public class PersistCollectionEditor extends CustomCollectionEditor
{
	private ServiceFactory serviceFactory;

	private Class<? extends AbstractEntity> type;

	@SuppressWarnings("unchecked")
	public PersistCollectionEditor(Class<? extends Collection> class1, Class<? extends AbstractEntity> type,
			ServiceFactory serviceFactory)
	{
		super(class1);
		this.serviceFactory = serviceFactory;
		this.type = type;
	}

	@Override
	protected Object convertElement(Object element)
	{
		if (element instanceof AbstractEntity)
		{
			System.out.println("Converting from entity to entity: " + element);
			return element;
		}
		if (element instanceof String)
		{
			// AppRole staff = staffCache.get(element);
			AbstractEntity role = serviceFactory.getPersistService().get(type, Long.parseLong(element.toString()));
			System.out.println("Looking up entity for id " + element + ": " + role);
			return role;
		}
		System.out.println("Don't know what to do with: " + element);
		return null;
	}
}
